//
//  BLNavigationController.m
//  Byelone
//
//  Created by 乔小乔 on 2020/12/14.
//

#import "BLNavigationController.h"

@interface BLNavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>{
    
}

@end

@implementation BLNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBar.translucent = NO;
    self.interactivePopGestureRecognizer.delegate = self;
    self.delegate = self;
    self.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationBar.shadowImage = [UIImage new];

    if (@available(iOS 8.2, *)) {
        [self.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor chColorWithHexString:@"#1B1C33"], NSForegroundColorAttributeName, [UIFont systemFontOfSize:16 weight:UIFontWeightMedium], NSFontAttributeName, nil]];
    } else {
        [self.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor chColorWithHexString:@"#1B1C33"], NSForegroundColorAttributeName, [UIFont boldSystemFontOfSize:16], NSFontAttributeName, nil]];
    }

    if (@available(iOS 11.0, *)){
        self.navigationBar.prefersLargeTitles = NO;
        self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
        [self.navigationBar setLargeTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, [UIFont boldSystemFontOfSize:30], NSFontAttributeName, nil]];
    }else{
        UIImage *image = [UIImage imageNamed:@"nav_bar_white"];
        [self.navigationBar setBackgroundImage:image forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];

    }
}



#pragma mark - pop & push
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    NSArray *array = self.viewControllers;
    if (array.count == 1) {
        return NO;
    }
    return YES;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.viewControllers.count >= 1) {
        // push时候隐藏tabBar
   
             viewController.hidesBottomBarWhenPushed = YES;
       
    }
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    [super pushViewController:viewController animated:animated];
}

 

- (void)setViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers animated:(BOOL)animated
{
    self.navigationBar.hidden = !self.navigationBar.hidden;//据说是防止navi bar出现重叠问题
    if (viewControllers.count > 1) {
        [[viewControllers firstObject] setHidesBottomBarWhenPushed:YES];
    }
    else {
        [[viewControllers firstObject] setHidesBottomBarWhenPushed:NO];
    }
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    [super setViewControllers:viewControllers animated:animated];
    self.navigationBar.hidden = !self.navigationBar.hidden;//据说是防止navi bar出现重叠问题
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    if (self.viewControllers.count <= 2) {
        [[self.viewControllers firstObject] setHidesBottomBarWhenPushed:NO];
    }
    return [super popViewControllerAnimated:animated];
}

- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (self.viewControllers.count <= 2) {
        [[self.viewControllers firstObject] setHidesBottomBarWhenPushed:NO];
    }
    return [super popToViewController:viewController animated:animated];
}

- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
    [[self.viewControllers firstObject] setHidesBottomBarWhenPushed:NO];
    return [super popToRootViewControllerAnimated:animated];
}




#pragma mark - UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    BOOL hideBool = NO;
    NSString *vcStr = NSStringFromClass([viewController class]);
    NSArray *array1 = [self getHiddenNavigation];
    if ([array1 containsObject:vcStr]) {
        hideBool = YES;
    }
    [self setNavigationBarHidden:hideBool animated:YES];
}

- (NSArray *)getHiddenNavigation{
  return NoneNavViewControllers;
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
        self.interactivePopGestureRecognizer.enabled = YES;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController  animationControllerForOperation:(UINavigationControllerOperation)operation  fromViewController:(UIViewController *)fromVC  toViewController:(UIViewController *)toVC
{
    //    if ([fromVC isKindOfClass:[CNBasicWebViewController class]]) {
    //        ((CNBasicWebViewController *)fromVC).op = operation;
    //    }
    return nil;
}





- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}



//-(UIViewController*)childViewControllerForStatusBarStyle{
//    NSLog(@"%@",self.navigationController);
//    NSLog(@"%@",self.self.childViewControllers);
//    return self.viewControllers.lastObject;
//}

- (void)dealloc
{
    self.delegate = nil;
    self.interactivePopGestureRecognizer.delegate = nil;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
